package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.my160920.picker.TimePicker;
import com.mission.schedule.my160920.widget.EditTextC;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * 日程内容编辑对话框
 * Created by liwei on 2017/4/18.
 */

public class MonthActivityAddDialog extends Dialog {

    CallBack callBack;

    SharedPrefUtil sharedPrefUtil;
    Context context;
    WindowManager windowManager;
    EditTextC edit_add;
    InputMethodManager imm;
    View view;
    TextView paste_tv, all_today, time_tv, cancel, ok;
    String date_str;
    TimePicker timePicker;

    public MonthActivityAddDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final String date,EditTextC edit_add) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        this.edit_add = edit_add;

        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.dialog_add_monthview_calender, null);
        setContentView(view);

        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        date_str = date;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        paste_tv = view.findViewById(R.id.paste_tv);
        all_today = view.findViewById(R.id.all_today);
        time_tv = view.findViewById(R.id.time_tv);
        cancel = view.findViewById(R.id.cancel);
        ok = view.findViewById(R.id.ok);
        timePicker = view.findViewById(R.id.timePicker);

        time_tv.setText(DateUtil.nowTimes());

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        paste_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        all_today.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        time_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        imm.showSoftInput(edit_add, 0);

    }


    String schTime = "23:59";
    int schDisplayTime = 0;

    void save() {
        dismiss();
        if (!edit_add.getText().toString().isEmpty()) {
            App.getDBcApplication().insertScheduleData(edit_add.getText().toString(),
                    date_str, schTime, 0, 0,
                    schDisplayTime, 1, 0, 0, 0,
                    date_str + " " + schTime + ":00", "",
                    2, DateUtil.nowTime2(), "0", 0, "", "",
                    1, 0, 0, "完成任务", "g_88", "", 0, 0, 0, "", "", 0, 0, 0, 0, "0", "0", "0");
            QueryAlarmData.writeAlarm(context);
            Toast.makeText(context, "新建成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Const.SHUAXINDATA);
            intent.putExtra("data", "success");
            intent.putExtra("index", 1);
            intent.putExtra("what", 2);
            context.sendBroadcast(intent);
            sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.IS_MONTH_RE, "1");

            callBack.reminderEditTime();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /* 触摸外部弹窗 */
        if (isOutOfBounds(getContext(), event)) {
            dismiss();
            return false;
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void reminderEditTime();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
